/**
 * Created by l on 2016/4/21.
 */
/**
 * Module dependencies.
 */
var CustomerLinkDealUser = require('../../models/customer/CustomerLinkDealUser.js')
    , beanFactory = require('../../beanFactory.js')
    , tool = require('../../../../shared/tool.js')
    , async = require('async')
    , uuid = require('uuid')
    , logger = require('pomelo-logger').getLogger(__filename);

/**
 *
 *
 * @api public
 */
function CustomerLinkDealUserService() {}

/**
 *
 *
 * @return {[type]}
 */
CustomerLinkDealUserService.prototype.init = function() {
    logger.info("CustomerLinkDealUserService init");
};

/**
 *
 * @param dealUserId
 * @param customerId
 * @param next
 */
CustomerLinkDealUserService.prototype.save = function(dealUserId, customerId, next){

    var self = this;
    var customerService = beanFactory.getBean("customerService");
    var dealUserService = beanFactory.getBean("dealUserService");
    if(tool.isNotNull(dealUserId)){
        async.waterfall([
            function(cb){
                var option = {
                    where: {
                        customerId: customerId,
                        dealUserId: dealUserId
                    }
                };
                self.getInfoByOption(option, function(err, customerDealUser){
                    if(customerDealUser){
                        next(null, customerDealUser);
                    } else {
                        cb();
                    }
                });
            },
            function(cb){
                customerService.getCustomerById(customerId, function(err, customer){
                    if(customer){
                        dealUserService.baseNamed(dealUserId, customer.name, customer.title, customer.email
                            , customer.company, customer.phone, function(err, dealUser){
                                logger.info("dealUser_base_named result: %j", dealUser);
                                cb();
                            });
                    } else {
                        cb();
                    }
                });
            },
            function(){
                var obj = {
                    id: uuid.v4(),
                    customerId: customerId,
                    dealUserId: dealUserId
                };

                CustomerLinkDealUser.create(obj, function(err, obj){
                    if(err){
                        logger.info("error in Customer#create %j", err);
                        return next(err);
                    }

                    next(null, obj);
                });
            }
        ]);
    } else {
        next(null, null);
    }
};

CustomerLinkDealUserService.prototype.getInfoByOption = function(option, next){
    CustomerLinkDealUser.findOne(option, function(err, customerDealUser){
        if(err){
            logger.error("get_info by option: %j failed: %j", option, err);
            return next(err);
        }
        logger.info("get_info by option: %j result: %j", option, customerDealUser);
        next(null, customerDealUser);
    });
};

CustomerLinkDealUserService.prototype.getDealUserCustomer = function(dealUserId, next){
    var option = {
        where: {
            dealUserId: dealUserId
        }
    };
    CustomerLinkDealUser.findOne(option, function(err, res){
        if(err){
            logger.info("get customer link deal user by dealUserId failed: %j", err);
            return next(err);
        }

        logger.info("is customer link deal user res: %s", res);
        next(null, res);
    });
};

CustomerLinkDealUserService.prototype.getDealUserCustomers = function(dealUserId, creatorId, next){

    var customerLinkDealUserDao = beanFactory.getBean("customerLinkDealUserDao");
    customerLinkDealUserDao.getDealUserCustomers(dealUserId, creatorId, function(err, res){
        if(err){
            logger.info("get customer link deal user by dealUserId failed: %j", err);
            return next(err);
        }

        logger.info("is customer link deal users res: %s", res);
        next(null, res);
    });
};

CustomerLinkDealUserService.prototype.getDealUserIds = function(customerId, next){
    var option = {
        where: {
            customerId: customerId
        }
    };

    var result = [];

    CustomerLinkDealUser.all(option, function(err, res){
        if(err){
            logger.info("get customer link deal users by customerId failed: %j", err);
            return next(err);
        }

        if(res && res.length > 0){
            for(var i=0; i < res.length; i++){
                var customerLinkDealUser = res[i];
                var dealUserId = customerLinkDealUser.dealUserId;
                if(tool.isNotNull(dealUserId)){
                    result.push(dealUserId);
                }
            }
        }
        next(null, result);
    });
};

/**
 * Expose `CustomerLinkDealUserService`.
 */
module.exports = CustomerLinkDealUserService;